package com.auoem.radio.common.custview.wheel;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;

import com.auctus.am.framework.picker.IIntPickDataWrap;
import com.auoem.radio.R;
import com.auoem.radio.common.custview.wheel.adapter.ControlWheelAdapter;


public class ThreePickerDialog extends Dialog implements OnWheelChangedListener {

    private final static int DEFAULT_ITEMS = 5;
    private final static int UPDATE_CITY_WHEEL = 11;
    private final static int UPDATE_COUNTY_WHEEL = 12;

    public final static int INDEX_UNPICK = -2;

    private Activity mContext;

    OnPickedListener mListener;

    IIntPickDataWrap mPickData1, mPickData2, mPickData3;

    ControlWheelAdapter mPickDataAdapter1;
    ControlWheelAdapter mPickDataAdapter2;
    ControlWheelAdapter mPickDataAdapter3;

    View done, cancel;

    WheelView wheel1;
    WheelView wheel2;
    WheelView wheel3;

    int index1, index2, index3;
    int v1, v2, v3;

    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        Log.d("ThreePickerDialog", String.format("onChanged , wheel:%d old:%d new:%d", wheel.getId(), oldValue, newValue));
        switch (wheel.getId()) {
            case R.id.wheel1:
                if (newValue != oldValue) {
//                    if (mPickDataAdapter2 == null) {
//                        return;//第一次初始为空
//                    }
                    index1 = newValue;
                    int v1 = mPickData1.getValue(index1);
                    mListener.onWheelPick(1, v1, index1);
//                    wheel2.invalidateWheel(true);
                }
                break;
            case R.id.wheel2:
                if (newValue != oldValue) {
                    index2 = newValue;
                    v2 = mPickData2.getValue(index2);
                    mListener.onWheelPick(2, v2, index2);
//                    wheel3.invalidateWheel(true);
                }
                break;
            case R.id.wheel3:
                if (newValue != oldValue) {
                    index3 = newValue;
                    v3 = mPickData3.getValue(index3);
                    mListener.onWheelPick(3, v3, index3);
                }
                break;
        }
    }

    public interface OnPickedListener {
        void onPicked(int sel1, int v1, int sel2, int v2, int sel3, int v3);

        void onWheelPick(int whichWheel, int value, int index);
    }


    private void initViews() {
        wheel1 = findViewById(R.id.wheel1);
        wheel2 = findViewById(R.id.wheel2);
        wheel3 = findViewById(R.id.wheel3);

        wheel1.setCyclic(false);
        wheel1.setVisibleItems(DEFAULT_ITEMS);

        wheel2.setCyclic(false);
        wheel2.setVisibleItems(DEFAULT_ITEMS);

        wheel3.setCyclic(false);
        wheel3.setVisibleItems(DEFAULT_ITEMS);

        OnWheelClickedListener clickListener = new OnWheelClickedListener() {
            @Override
            public void onItemClicked(WheelView wheel, int itemIndex) {
                if (itemIndex != wheel.getCurrentItem()) {
                    wheel.setCurrentItem(itemIndex, true, 500);
                }
            }
        };

        wheel1.addClickingListener(clickListener);
        wheel2.addClickingListener(clickListener);
        wheel3.addClickingListener(clickListener);

        wheel1.setChangingListener(this);
        wheel2.setChangingListener(this);
        wheel3.setChangingListener(this);
    }

    public ThreePickerDialog(Activity context,
                             OnPickedListener listener) {
        super(context);
        mContext = context;
        mListener = listener;
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        getWindow().setGravity(Gravity.BOTTOM);
        getWindow().setBackgroundDrawable(
                new ColorDrawable(Color.parseColor("#00000000")));
        getWindow().setWindowAnimations(R.style.AnimBottom);
        View rootView = getLayoutInflater().inflate(
                R.layout.custview_wheel_dialog_three_picker, null);
        int screenWidth = mContext.getWindowManager().getDefaultDisplay()
                .getWidth();
        LayoutParams params = new LayoutParams(screenWidth,
                LayoutParams.MATCH_PARENT);
        super.setContentView(rootView, params);

        initViews();

        View done = findViewById(R.id.done);
        done.setOnClickListener(v -> {
            if (mListener != null) {
                mListener.onPicked(index1, mPickData1.getValue(index1), index2, mPickData2 != null ? mPickData2.getValue(index2) : 0, index3, mPickData3 != null ? mPickData3.getValue(index3) : 0);
            }
            dismiss();
        });

        View cancel = findViewById(R.id.cancel);
        cancel.setOnClickListener(v -> {
            mListener.onPicked(INDEX_UNPICK, 0, 0, 0, 0, 0);
            dismiss();
        });
        setOnCancelListener(dialog -> mListener.onPicked(INDEX_UNPICK, 0, 0, 0, 0, 0));
    }

    public ThreePickerDialog(Activity context) {
        super(context);
        mContext = context;
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        getWindow().setGravity(Gravity.BOTTOM);
        getWindow().setBackgroundDrawable(
                new ColorDrawable(Color.parseColor("#00000000")));
        getWindow().setWindowAnimations(R.style.AnimBottom);
        View rootView = getLayoutInflater().inflate(
                R.layout.custview_wheel_dialog_three_picker, null);
        int screenWidth = mContext.getWindowManager().getDefaultDisplay()
                .getWidth();
        LayoutParams params = new LayoutParams(screenWidth,
                LayoutParams.MATCH_PARENT);
        super.setContentView(rootView, params);

        initViews();

        done = findViewById(R.id.done);
        cancel = findViewById(R.id.cancel);
    }

    public void setOnPickedListener(OnPickedListener onPickedListener) {
        mListener = onPickedListener;
        done.setOnClickListener(v -> {
            if (mListener != null) {
                mListener.onPicked(index1, mPickData1.getValue(index1), index2, mPickData2 != null ? mPickData2.getValue(index2) : 0, index3, mPickData3 != null ? mPickData3.getValue(index3) : 0);
            }
            dismiss();
        });
        cancel.setOnClickListener(v -> {
            mListener.onPicked(INDEX_UNPICK, 0, 0, 0, 0, 0);
            dismiss();
        });
        setOnCancelListener(dialog -> mListener.onPicked(INDEX_UNPICK, 0, 0, 0, 0, 0));
    }

    //    @SuppressLint("DefaultLocale")
    public void update(IIntPickDataWrap pickData1, int v1, IIntPickDataWrap pickData2, int v2, IIntPickDataWrap pickData3, int v3) {
        mPickData1 = pickData1;
        if (mPickDataAdapter1 == null) {
            mPickDataAdapter1 = new ControlWheelAdapter(mContext, mPickData1);
        } else {
            mPickDataAdapter1.update(mPickData1, 0);
        }
        wheel1.setViewAdapter(mPickDataAdapter1);
        wheel1.setCyclic(false);
        wheel1.setVisibleItems(DEFAULT_ITEMS);
        int index1 = mPickData1.getIndexOnValue(v1);
        if (index1 >= 0 && index1 < mPickData1.getSize()) {
            wheel1.setCurrentItem(index1);
        } else {
            wheel1.setCurrentItem(0);
        }

        mPickData2 = pickData2;
        if (mPickData2 != null) {
            wheel2.setVisibility(View.VISIBLE);
            if (mPickDataAdapter2 == null) {
                mPickDataAdapter2 = new ControlWheelAdapter(mContext, mPickData2);
            } else {
                mPickDataAdapter2.update(mPickData2, 0);
            }
            wheel2.setViewAdapter(mPickDataAdapter2);
            wheel2.setCyclic(false);
            wheel2.setVisibleItems(DEFAULT_ITEMS);
            int index2 = mPickData2.getIndexOnValue(v2);
            if (index2 >= 0 && index2 < mPickData2.getSize()) {
                wheel2.setCurrentItem(index2);
            } else {
                wheel2.setCurrentItem(0);
            }
        } else {
            wheel2.setVisibility(View.GONE);
        }

        mPickData3 = pickData3;
        if (mPickData3 != null) {
            wheel3.setVisibility(View.VISIBLE);
            if (mPickDataAdapter3 == null) {
                mPickDataAdapter3 = new ControlWheelAdapter(mContext, mPickData3);
            } else {
                mPickDataAdapter3.update(mPickData3, 0);
            }
            wheel3.setViewAdapter(mPickDataAdapter3);
            wheel3.setCyclic(false);
            wheel3.setVisibleItems(DEFAULT_ITEMS);
            int index3 = mPickData3.getIndexOnValue(v3);
            if (index3 >= 0 && index3 < mPickData2.getSize()) {
                wheel3.setCurrentItem(index3);
            } else {
                wheel3.setCurrentItem(0);
            }
        } else {
            wheel3.setVisibility(View.GONE);
        }
        Log.d("ChannelPickerDialog", String.format("update , v1:%d v2:%d v3:%d", v1, v2, v3));
    }

    public void update(int whichWheel, IIntPickDataWrap pickData, int v1, int index) {
        if (whichWheel == 1) {
            mPickData1 = pickData;
            if (mPickDataAdapter1 == null) {
                mPickDataAdapter1 = new ControlWheelAdapter(mContext, mPickData1);
            } else {
                mPickDataAdapter1.update(mPickData1, 0);
            }
            wheel1.setViewAdapter(mPickDataAdapter1);
            wheel1.setCyclic(false);
            wheel1.setVisibleItems(DEFAULT_ITEMS);
            int index1 = mPickData1.getIndexOnValue(v1);
            if (index1 >= 0 && index1 < mPickData1.getSize()) {
                wheel1.setCurrentItemNotify(index1);
            } else {
                wheel1.setCurrentItemNotify(0);
            }
//            wheel1.invalidateWheel(true);
        } else if (whichWheel == 2) {
            mPickData2 = pickData;
            if (mPickDataAdapter2 == null) {
                mPickDataAdapter2 = new ControlWheelAdapter(mContext, mPickData2);
            } else {
                mPickDataAdapter2.update(mPickData2, 0);
            }
            wheel2.setViewAdapter(mPickDataAdapter2);
            wheel2.setCyclic(false);
            wheel2.setVisibleItems(DEFAULT_ITEMS);
            int index2 = mPickData2.getIndexOnValue(v2);
            if (index2 >= 0 && index2 < mPickData2.getSize()) {
                wheel2.setCurrentItemNotify(index2);
            } else {
                wheel2.setCurrentItemNotify(0);
            }
//            wheel2.invalidateWheel(true);
        } else {
            mPickData3 = pickData;
            if (mPickDataAdapter3 == null) {
                mPickDataAdapter3 = new ControlWheelAdapter(mContext, mPickData3);
            } else {
                mPickDataAdapter3.update(mPickData3, 0);
            }
            wheel3.setViewAdapter(mPickDataAdapter3);
            wheel3.setCyclic(false);
            wheel3.setVisibleItems(DEFAULT_ITEMS);
            int index3 = mPickData3.getIndexOnValue(v3);
            if (index3 >= 0 && index3 < mPickData2.getSize()) {
                wheel3.setCurrentItem(index3);
            } else {
                wheel3.setCurrentItem(0);
            }
//            wheel3.invalidateWheel(true);
        }
    }

}
